package cn.core.dict.rest;

import cn.core.dict.eo.Dict;
import cn.core.dict.qo.DictQO;
import cn.core.dict.service.DictService;
import cn.core.dict.vo.DictAndValueVO;
import cn.core.dict.vo.DictVO;
import cn.core.tool.exception.CoreException;
import cn.core.tool.rest.CoreController;
import cn.core.tool.util.CoreTool;
import cn.core.tool.util.Param;
import cn.core.tool.util.Res;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedHashMap;

/**
 * 数据字典 REST API
 */
@Log4j2
@RestController
@Scope("prototype")
@RequestMapping("dict/dict")
public class DictController extends CoreController {

  public static final String name = "数据字典";

  @Resource
  private DictService dictService;

  /**
   * 数据字典 >>> 保存
   * @param param 参数
   * @return 返回
   */
  @PostMapping(save)
  public Res<Dict> save(@RequestBody Param<Dict> param) throws Exception {
    Res<Dict> res = new Res<>(name + succSave);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(dictService.save(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 数据字典 >>> 批量保存
   * @param param 参数
   * @return 返回
   */
  @PostMapping(saveBatch)
  public Res<Dict> saveBatch(@RequestBody Param<Dict> param) throws Exception {
    Res<Dict> res = new Res<>(name + succSave);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(dictService.saveBatch(param.getData()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 数据字典 >>> 修改
   * @param param 参数
   * @return 返回
   */
  @PostMapping(update)
  public Res<Dict> update(@RequestBody Param<Dict> param) throws Exception {
    Res<Dict> res = new Res<>(name + succUpdate);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(dictService.update(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 数据字典 >>> 批量修改
   * @param param 参数
   * @return 返回
   */
  @PostMapping(updateBatch)
  public Res<Dict> updateBatch(@RequestBody Param<Dict> param) throws Exception {
    Res<Dict> res = new Res<>(name + succUpdate);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(dictService.updateBatch(param.getData()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 数据字典 >>> 删除
   * @param param 参数
   * @return 返回
   */
  @PostMapping(remove)
  public Res<Dict> remove(@RequestBody Param<Dict> param) throws Exception {
    Res<Dict> res = new Res<>(name + succRemove);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      dictService.remove(param.get());
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 数据字典 >>> 批量删除
   * @param param 参数
   * @return 返回
   */
  @PostMapping(removeBatch)
  public Res<Dict> removeBatch(@RequestBody Param<Dict> param) throws Exception {
    Res<Dict> res = new Res<>(name + succRemove);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      dictService.removeBatch(param.getData());
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 数据字典 >>> 数据量
   * @param param 参数
   * @return 返回
   */
  @PostMapping(count)
  public Res<Dict> count(@RequestBody Param<DictQO> param) throws Exception {
    Res<Dict> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setCount(dictService.count(null));
      } else {
        res.setCount(dictService.count(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 数据字典 >>> 主键查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(pk)
  public Res<Dict> pk(@RequestBody Param<DictQO> param) throws Exception {
    Res<Dict> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(dictService.pk(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 数据字典 >>> 查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(query)
  public Res<Dict> query(@RequestBody Param<DictQO> param) throws Exception {
    Res<Dict> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setData(dictService.query(null));
      } else {
        res.setData(dictService.query(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 数据字典 >>> 分页
   * @param param 分页参数
   * @return 返回
   */
  @PostMapping(paging)
  public Res<Dict> paging(@RequestBody Param<DictQO> param) throws Exception {
    Res<Dict> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(dictService.paging(param));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 数据字典 >>> 业务数据量
   * @param param 参数
   * @return 返回
   */
  @PostMapping(countVo)
  public Res<DictVO> countVo(@RequestBody Param<DictQO> param) throws Exception {
    Res<DictVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setCount(dictService.countVo(null));
      } else {
        res.setCount(dictService.countVo(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 数据字典 >>> 业务主键查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(pkVo)
  public Res<DictVO> pkVo(@RequestBody Param<DictQO> param) throws Exception {
    Res<DictVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(dictService.pkVo(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 数据字典 >>> 业务查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(queryVo)
  public Res<DictVO> queryVo(@RequestBody Param<DictQO> param) throws Exception {
    Res<DictVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setData(dictService.queryVo(null));
      } else {
        res.setData(dictService.queryVo(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 数据字典 >>> 业务分页
   * @param param 分页参数
   * @return 返回
   */
  @PostMapping(pagingVo)
  public Res<DictVO> pagingVo(@RequestBody Param<DictQO> param) throws Exception {
    Res<DictVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(dictService.pagingVo(param));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 数据字典标识 >>> 数据字典和数据字典值信息
   * @param param 参数
   * @return 返回
   */
  @PostMapping("getDictAndValueByDictKey")
  public Res<DictAndValueVO> getDictAndValueByDictKey(@RequestBody Param<DictQO> param) throws Exception {
    Res<DictAndValueVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(dictService.getDictAndValueByDictKey(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 数据字典 >>> 获取所有数据字典和数据字典值键值信息
   * @param param 参数
   * @return 返回
   */
  @PostMapping("getAllDictValueMapInfo")
  public Res<LinkedHashMap<String, LinkedHashMap<String, String>>> getAllDictValueMapInfo(@RequestBody Param<?> param) throws Exception {
    Res<LinkedHashMap<String, LinkedHashMap<String, String>>> res = new Res<>();
    try {
      res.setData(dictService.getAllDictValueMapInfo());
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

}
